package com.homevip.user.dao;

import com.homevip.core.dao.BaseDaoWithQuery;
import com.homevip.core.dao.QueryAdded;
import com.homevip.core.page.Page;
import com.homevip.core.page.Pageable;
import com.homevip.user.entity.User;
import com.homevip.util.SqlHelper;
import com.homevip.util.StringUtil;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户Dao
 *
 * @author panpan
 * @version 2014-12-18
 */
@Repository
public class UserDao extends BaseDaoWithQuery<User, Integer> {
    @PersistenceContext
    private EntityManager em;

    public UserDao() {
        super(User.class);
    }

    @Override
    protected EntityManager getEntityManager() {
        // TODO Auto-generated method stub
        return em;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return super.toString();
    }

    @Override
    protected void finalize() throws Throwable {
        // TODO Auto-generated method stub
        super.finalize();
    }

    protected UserDao(Class<User> entityClass) {
        super(entityClass);
    }

    // 查询账号姓名和状态
    public Page<User> findBy(String keyword, Integer usertype, Integer status, Pageable pageable) {
        StringBuilder sb = new StringBuilder(128);
        ArrayList<Object> params = new ArrayList<Object>();
        sb.append("1=1");

        if (!StringUtil.isEmpty(keyword)) {
            keyword = SqlHelper.toJpqlParamLike(keyword.trim());
            sb.append(" and (account like ? or cname like ?)");
            params.add(keyword.trim());
            params.add(keyword.trim());
        }
        if (null != usertype && usertype > 0) {
            sb.append(" and usertype=?");
            params.add(usertype);
        }
        if (null != status && status > 0) {
            sb.append(" and status=?");
            params.add(status);
        }
        return queryBy(sb.toString(), params.toArray(), pageable);
    }

    // 查找某组的人
    public Page<User> findByWithGroupId(String keyword, Integer status, Integer groupid, Pageable pageable) {
        StringBuilder sb = new StringBuilder(128);

        sb.append(" and b.groupid=").append(groupid);
        if (null != status && status > 0) {
            sb.append(" and d.status=").append(status);
        }
        if (null != keyword) {
            keyword = SqlHelper.toSqlLike(keyword);
            sb.append(" and d.cname like ").append(keyword);
        }
        String strQuery = "select count(d) from User d , User_group_mapping b where d.id = b.userid" + sb.toString();
        int nTotal = intSelect(strQuery);
        // 再查询
        strQuery = " select d, b.id, b.groupid, b.type from User d , User_group_mapping b where d.id = b.userid"
                + sb.toString();
        QueryAdded<User> qa = new QueryAdded<User>(em, strQuery, pageable);
        return qa.getPage(nTotal, "ugmid", "groupid", "groupusertype");
    }

    // 查找某角色的所有组长
    public List<User> findByWithType(Integer type, Integer status, Integer userid) {
        StringBuilder sb = new StringBuilder();
        String strQuery = "select d from User d,User_group g where d.id=g.supervisorid";
        if (type != null) {
            sb.append(" and g.type=" + type);
        }
        if (status != null) {
            sb.append(" and g.status=" + status);
        }
        if (userid != null) {
            sb.append(" and d.id=" + userid);
        }
        return queryBy(strQuery + sb.toString());
    }

    // 查找组长
    public Page<User> findLeaderBy(String keyword, Integer status, Integer groupid, Pageable pageable) {
        StringBuilder sb = new StringBuilder(128);

        sb.append(" and b.type=2");
        if (null != keyword) {
            keyword = SqlHelper.toSqlLike(keyword);
            sb.append(" and ( d.cname like ").append(keyword);
            sb.append(" or d.code like ").append(keyword);
            sb.append(" )");
        }
        if (groupid > 0) {
            sb.append(" and b.groupid=").append(groupid);
        }

        String strQuery = "select count(d) from User d , User_group_mapping b, User_group c where d.id = b.userid and b.groupid=c.id"
                + sb.toString();
        int nTotal = intSelect(strQuery);
        // 再查询
        strQuery = " select d, b.groupid, b.type, c.cname from User d , User_group_mapping b, User_group c where d.id = b.userid and b.groupid=c.id"
                + sb.toString();
        QueryAdded<User> qa = new QueryAdded<User>(em, strQuery, pageable);
        return qa.getPage(nTotal, "groupid", "groupusertype", "groupname");
    }

    /**
     * 查找工人
     *
     * @param ids           编号集
     * @param serviceItemId 服务项编号
     * @return
     */
    public List<User> findWorkderBy(List<Integer> ids, int serviceItemId) {

        StringBuilder sb = new StringBuilder(128);
        // sb.append("select d from User d, user_servicesubitem_mapping r");
        // sb.append(" where d.id=r.users_id and d.id in(?1) and
        // r.servicesubitems_id=?2");
        sb.append("select d from User d inner join fetch d.serviceitems r");
        sb.append(" where d.id in(?1) and r.id=?2");

        TypedQuery<User> qa = getEntityManager().createQuery(sb.toString(), User.class);
        qa.setParameter(1, ids);
        qa.setParameter(2, serviceItemId);
        return qa.getResultList();
    }

}
